package two.chapter_3.c3_5;

import two.chapter_3.c3_4.SeparateChainingHashST;

public class SparseVector {

    private SeparateChainingHashST<Integer,Double> st;

    public SparseVector(){
        st=new SeparateChainingHashST<>();
    }

    public int size(){
        return st.size();
    }

    public void put(int i,double x){
        st.put(i,x);
    }
    public double get(int i){
        if (!st.contains(i)){
            return 0.0;
        }
        return st.get(i);
    }

    /**
     * 向量与向量的点积
     * @param that
     * @return
     */
    public double dot(double [] that){
        double sum=0.0;
        for (int i: st.keys()){
            sum+=that[i]*this.get(i);
        }
        return sum;
    }


}
